package logic

import (
	"C/Rpc/model/mysql"
	"context"
	"fmt"

	"C/Rpc/internal/svc"
	"C/Rpc/shop"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodListLogic {
	return &GoodListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商品列表
func (l *GoodListLogic) GoodList(in *shop.GoodListRequest) (*shop.GoodListResponse, error) {
	// todo: add your logic here and delete this line
	g := mysql.Good{}
	good, err := g.GetGood(int(in.Page), int(in.PageSize))
	if err != nil {
		return nil, fmt.Errorf("查询失败")
	}
	var list []*shop.GoodItem
	for _, g2 := range good {
		list = append(list, &shop.GoodItem{
			UserId:     int64(g2.UserId),
			GoodsTitle: g2.GoodsTitle,
			GoodsStock: int64(g2.GoodsStock),
			GoodsImage: g2.GoodsImage,
			GoodsPrice: float32(g2.GoodsPrice),
			GoodsLike:  int64(g2.GoodsLike),
			Status:     int64(g2.Status),
			Id:         int64(g2.ID),
		})
	}
	return &shop.GoodListResponse{
		List: list,
	}, nil
}
